package com.hmall.order.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hmall.common.cliet.ItemFeignClient;
import com.hmall.common.cliet.UserFeignClient;
import com.hmall.common.pojo.Address;
import com.hmall.common.pojo.Item;
import com.hmall.order.Interceptor.ThreadLocalUtil;
import com.hmall.order.config.SnowFlake;
import com.hmall.order.mapper.OrderMapper;
import com.hmall.order.pojo.Order;
import com.hmall.order.pojo.OrderDetail;
import com.hmall.order.pojo.OrderLogistics;
import com.hmall.order.pojo.OrderParameter;
import com.hmall.order.service.IOrderDetailService;
import com.hmall.order.service.IOrderLogisticsService;
import com.hmall.order.service.IOrderService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.UUID;

@Service
public class OrderService extends ServiceImpl<OrderMapper, Order> implements IOrderService {

    @Autowired
    private ItemFeignClient client;

    @Autowired
    private IOrderService orderService;

    @Autowired
    private IOrderDetailService orderDetailService;
    @Autowired
    private UserFeignClient userClient;
    @Autowired
    private IOrderLogisticsService orderLogisticsService;

    @Override
    public Long submitOrder(OrderParameter orderParameter) {
        //根据雪花算法生成订单id
        //String orderId = UUID.randomUUID().toString().replace("-", "");
        SnowFlake snowFlake = new SnowFlake(0);
        long orderId = snowFlake.nextId();//通过雪花算法生成IDSnowFlake snowFlake = new SnowFlake(0);
        //long orderId = snowFlake.nextId();//通过雪花算法生成ID
        //根据itemId查询商品信息
        Item item = client.selectById(orderParameter.getItemId());
        //基于商品价格、购买数量计算商品总价：totalFee
        Long totalFee = item.getPrice() * orderParameter.getNum();
        //封装Order对象，初识status为未支付
        Order order = new Order();
        order.setId(orderId);
        order.setTotalFee(totalFee);
        order.setPaymentType(orderParameter.getPaymentType());
        order.setUserId(Long.valueOf(ThreadLocalUtil.getCurrentId()));
        order.setStatus(1);
        //将Order写入数据库tb_order表中
        orderService.save(order);
        //将商品信息、orderId信息封装为OrderDetail对象，写入tb_order_detail表
        OrderDetail orderDetail = new OrderDetail();
        orderDetail.setOrderId(orderId);
        orderDetail.setItemId(orderParameter.getItemId());
        orderDetail.setNum(orderParameter.getNum());
        orderDetail.setTitle(item.getName());
        orderDetail.setPrice(item.getPrice());
        orderDetail.setSpec(item.getSpec());
        orderDetail.setImage(item.getImage());
        orderDetailService.save(orderDetail);
        //根据addressId查询user-service服务，获取地址信息
        Address address = userClient.findAddressById(orderParameter.getAddressId());
        //将地址封装为OrderLogistics对象，写入tb_order_logistics表
        OrderLogistics orderLogistics = new OrderLogistics();
        orderLogistics.setOrderId(orderId);
        orderLogistics.setContact(address.getContact());
        orderLogistics.setPhone(address.getMobile());
        orderLogistics.setProvince(address.getProvince());
        orderLogistics.setCity(address.getCity());
        orderLogistics.setTown(address.getTown());
        orderLogistics.setStreet(address.getStreet());
        orderLogisticsService.save(orderLogistics);
        client.stock(orderParameter.getItemId(),orderParameter.getNum());
        return orderId;
    }
}
